﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB50MSUMData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        DataTable tbAB50OUTPUT;
        public int ThreadCount;
        public AB50MSUMData()
            : base()
        {
            tbAB50OUTPUT = new DataTable("AB50OUTPUT");
            tbAB50OUTPUT.Columns.Add("SUMYEAR", typeof(string));
            tbAB50OUTPUT.Columns.Add("SUMMONTH", typeof(string));
            tbAB50OUTPUT.Columns.Add("LSLIEFNR", typeof(string));
            tbAB50OUTPUT.Columns.Add("FACTORY", typeof(string));
            tbAB50OUTPUT.Columns.Add("BATCHES", typeof(int));
            tbAB50OUTPUT.Columns.Add("SUBQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("SUBYIELDQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("G10KGORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("OTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("GOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXDELAYDAYS", typeof(int));
            tbAB50OUTPUT.Columns.Add("LOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXLESSDAYS", typeof(int));
        }
        public AB50MSUMData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();

            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "SELECT DISTINCT LSLIEFNR,LSNAME1 AS FACTORY,ABPPOSTYP,"
                + "ABPSOLLMENGE5,ABPEAKG, ABPLIEFDAT,ABPERLDAT "
                + " FROM ABPOS P    LEFT JOIN LIEFDAT L ON   p.ABPLIEFNR=L.LSLIEFNR "
                + " WHERE ABPAUFART='GP' AND ABPERLKZ='J' AND ABPAUFKRS LIKE 'D%' "
                + " AND ABPERLDAT>='" + WBSRTDATE + "' AND ABPERLDAT<='" + WBENTDATE + "' "
                + " ORDER BY 1,2,3,4";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "AB50ABPOS";
                    SetDefault(tableLocal);
                    tbAB50OUTPUT.Clear();
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY;
                    int PCNUM;
                    double WBEWMTR;
                    DataRow dwOUTPUT;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary = new List<WGSummary>();
                    string SUMMONTH, SUMYEAR, LSLIEFNR;
                    SUMYEAR = WBSRTDATE.Substring(0, 4);
                    SUMMONTH = WBSRTDATE.Substring(4, 2);
                    int G10KGORDERS;
                    int dYear, dMonth, dDay, eYear, eMonth, eDay;
                    string ABPLIEFDAT, ABPERLDAT;
                    DateTime dDate, eDate;
                    bool hasdDate = false, haseDate = false;
                    bool flagnew = false;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        LSLIEFNR = dwTemp["LSLIEFNR"].ToString();
                        ABPLIEFDAT = dwTemp["ABPLIEFDAT"].ToString();
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        FACTORY = value;
                        #endregion
                        dwOUTPUT = null;
                        var query = from DataRow dwX in tbAB50OUTPUT.AsEnumerable()
                                    where dwX["LSLIEFNR"].ToString() == LSLIEFNR
                                    select dwX;

                        foreach (DataRow dwX in query)
                        {
                            dwOUTPUT = dwX;
                            flagnew = false;
                        }
                        if (dwOUTPUT == null)
                        {
                            flagnew = true;
                            dwOUTPUT = tbAB50OUTPUT.NewRow();
                            dwOUTPUT["BATCHES"] = 0;
                            dwOUTPUT["SUBQTY"] = 0.0;
                            dwOUTPUT["SUBYIELDQTY"] = 0.0;
                            dwOUTPUT["G10KGORDERS"] = 0;
                            dwOUTPUT["OTDORDERS"] = 0;
                            dwOUTPUT["GOTDORDERS"] = 0;
                            dwOUTPUT["MAXDELAYDAYS"] = 0;
                            dwOUTPUT["LOTDORDERS"] = 0;
                            dwOUTPUT["MAXLESSDAYS"] = 0;
                        }
                        hasdDate = false;
                        haseDate = false;
                        dDate = new DateTime(1, 1, 1);
                        eDate = new DateTime(1, 1, 1);
                        if (ABPLIEFDAT.Length == 8)
                        {
                            dYear = System.Convert.ToInt16(ABPLIEFDAT.Substring(0, 4));
                            dMonth = System.Convert.ToInt16(ABPLIEFDAT.Substring(4, 2));
                            dDay = System.Convert.ToInt16(ABPLIEFDAT.Substring(6, 2));
                            dDate = new DateTime(dYear, dMonth, dDay);
                            hasdDate = true;
                        }
                        ABPERLDAT = dwTemp["ABPERLDAT"].ToString();
                      
                        if (ABPERLDAT.Length == 8)
                        {
                            eYear = System.Convert.ToInt16(ABPERLDAT.Substring(0, 4));
                            eMonth = System.Convert.ToInt16(ABPERLDAT.Substring(4, 2));
                            eDay = System.Convert.ToInt16(ABPERLDAT.Substring(6, 2));
                            eDate = new DateTime(eYear, eMonth, eDay);
                            haseDate = true;
                        }
                        double ABPSOLLMENGE5= 0.01 * System.Convert.ToDouble(dwTemp["ABPSOLLMENGE5"].ToString());
                        dwOUTPUT["SUMYEAR"] = SUMYEAR;
                        dwOUTPUT["SUMMONTH"] = SUMMONTH;
                        dwOUTPUT["LSLIEFNR"] = LSLIEFNR;
                        dwOUTPUT["FACTORY"] = FACTORY;
                        dwOUTPUT["BATCHES"] = System.Convert.ToInt16(dwOUTPUT["BATCHES"].ToString()) + 1;
                        dwOUTPUT["SUBQTY"] = System.Convert.ToDouble(dwOUTPUT["SUBQTY"].ToString()) + ABPSOLLMENGE5;
                        dwOUTPUT["SUBYIELDQTY"] = System.Convert.ToDouble(dwOUTPUT["SUBYIELDQTY"].ToString()) + 0.01 * System.Convert.ToDouble(dwTemp["ABPEAKG"].ToString());

                        if (ABPSOLLMENGE5 >= 10.0)
                        {
                            dwOUTPUT["G10KGORDERS"] = System.Convert.ToInt16(dwOUTPUT["G10KGORDERS"].ToString()) + 1;
                        }
                        else
                        {
                            continue;
                        }
                        if (hasdDate && haseDate)
                        {
                            int daysdiff = (eDate - dDate).Days;
                            if (daysdiff > 0)//delay
                            {
                                dwOUTPUT["GOTDORDERS"] = System.Convert.ToInt16(dwOUTPUT["GOTDORDERS"].ToString()) + 1;

                                int MAXDELAYDAYS = System.Convert.ToInt16(dwOUTPUT["MAXDELAYDAYS"].ToString());
                                if (daysdiff > MAXDELAYDAYS)
                                {
                                    dwOUTPUT["MAXDELAYDAYS"] = daysdiff;
                                }
                          
                                
                            }
                            if (daysdiff==0)//on time
                            {
                                dwOUTPUT["OTDORDERS"] = System.Convert.ToInt16(dwOUTPUT["OTDORDERS"].ToString()) + 1;
                            }
                            if (daysdiff<0)// advance
                            {
                                dwOUTPUT["LOTDORDERS"] = System.Convert.ToInt16(dwOUTPUT["LOTDORDERS"].ToString()) + 1;
                                int MAXLESSDAYS = System.Convert.ToInt16(dwOUTPUT["MAXLESSDAYS"].ToString());
                                if (System.Math.Abs(daysdiff) > MAXLESSDAYS)
                                {
                                    dwOUTPUT["MAXLESSDAYS"] = System.Math.Abs(daysdiff);
                                }
                            }
                        }
                        if (flagnew)
                        {
                            tbAB50OUTPUT.Rows.Add(dwOUTPUT);
                        }
                    }

                    setLocal.AcceptChanges();
                    set4Return.Merge(setLocal);
                    set4Return.Merge(tbAB50OUTPUT);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}